﻿using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.Windows.Automation;
using System.Windows.Forms;
using XiaofangExamFramework.ServiceApi;

namespace XiaofangExamFramework
{
    public partial class Form1 : Form
    {
        Form4 form4;
        MouseHook hook;
        public Form1()
        {
            InitializeComponent();
        }

        #region 鼠标 键盘事件
        private void MouseDown(object sender, MouseEventArgs e)
        {

        }
        string oldtext;
        private void MouseUp(object sender, MouseEventArgs e)
        {
            Debug.WriteLine(string.Format("在{0},{1}位置松开了鼠标{2}键", e.X, e.Y, e.Button.ToString()));

            TXDTO dto = getCurrentSelectTextByUIA();
            string text = dto.TiMu;
            if (!string.IsNullOrEmpty(text) && oldtext != text)
            {
                oldtext = text;
                string t = text.Replace(" ", "").Replace(((char)160).ToString(), string.Empty);
                Debug.WriteLine(t);

                var result = KaoshiApi.Search(t, dto.Name);
                Debug.WriteLine(JsonConvert.SerializeObject(result));
                if (result != null && result.Total > 0)
                {
                    ClosePopForm();
                    form4 = new Form4(text, result);
                    form4.Show();

                    timer2.Start();
                }
            }
        }

        /// <summary>
        /// 键盘抬起
        /// </summary>
        void hook_KeyUp(object sender, KeyEventArgs e)
        {
            Debug.WriteLine("键盘抬起, " + e.KeyData.ToString() + " 键码:" + e.KeyValue);
        }
        /// <summary>
        /// 键盘输入
        /// </summary>
        void hook_KeyPress(object sender, KeyPressEventArgs e)
        {
            Debug.WriteLine("键盘输入。");
        }
        /// <summary>
        /// 键盘按下
        /// </summary>
        void hook_KeyDown(object sender, KeyEventArgs e)
        {
            Debug.WriteLine("键盘按下, " + e.KeyData.ToString() + " 键码:" + e.KeyValue);
            if (e.KeyData.ToString() == "LShiftKey" || e.KeyData.ToString() == "RShiftKey")
            {
                ClosePopForm();
            }
        }

        #endregion

        private void ClosePopForm()
        {
            timer2.Stop();
            if (form4 != null)
            {
                form4.Hide();
                form4.Close();
                form4.Dispose();
            }
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            //初始化钩子对象
            if (hook == null)
            {
                hook = new MouseHook();
                hook.MouseDown += new MouseEventHandler(MouseDown);
                hook.MouseUp += new MouseEventHandler(MouseUp);

                hook.KeyDown += new KeyEventHandler(hook_KeyDown);
                hook.KeyPress += new KeyPressEventHandler(hook_KeyPress);
                hook.KeyUp += new KeyEventHandler(hook_KeyUp);
            }
            hook.Start();
        }
        private void timer2_Tick_1(object sender, EventArgs e)
        {
            ClosePopForm();
            oldtext = string.Empty;
        }

        private TXDTO getCurrentSelectTextByUIA()
        {
            string tigan = string.Empty;
            TXDTO dto = new TXDTO();
            try
            {
                //AutomationElement root = AutomationElement.RootElement;
                //PropertyCondition propertyCondition = new PropertyCondition(AutomationElement.NameProperty, "消防理论考试 - 山东盛恩技术人员技能考试 - Microsoft Edge");

                //PropertyCondition pc2 = new PropertyCondition(AutomationElement.NameProperty, "消防理论考试 - 山东盛恩技术人员技能考试");
                //PropertyCondition pc3 = new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "文档");
                //AutomationElement docmain = root.FindFirst(TreeScope.Descendants, new AndCondition(pc2, pc3));
                AutomationElement docmain = AutomationElement.FocusedElement;
                TreeWalker treeWalker = TreeWalker.RawViewWalker;

                var item = treeWalker.GetFirstChild(docmain);
                var item2 = treeWalker.GetFirstChild(item);
                var item3 = treeWalker.GetFirstChild(item2);
                var item4 = treeWalker.GetNextSibling(item3);
                var item5 = treeWalker.GetNextSibling(item4);


                var item6 = treeWalker.GetNextSibling(item5);

                var item7 = treeWalker.GetNextSibling(item6);
                var item8 = treeWalker.GetFirstChild(item7);
                var item9 = treeWalker.GetFirstChild(item8);
                var item10 = treeWalker.GetFirstChild(item9);
                var item11 = treeWalker.GetFirstChild(item10);
                PropertyCondition pctigan = new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "文本");

                var items = item11.FindAll(TreeScope.Children, pctigan);
                dto.TiMu = items[3].Current.Name;

                #region 获取姓名
                var xingming1 = treeWalker.GetNextSibling(item7);
                var xingming2 = treeWalker.GetFirstChild(xingming1);
                var xingming3 = treeWalker.GetFirstChild(xingming2);
                var xingming4 = treeWalker.GetFirstChild(xingming3);
                var xingming5 = treeWalker.GetFirstChild(xingming4);
                var xingming6 = treeWalker.GetFirstChild(xingming5);
                var xingming = treeWalker.GetLastChild(xingming6);
                dto.Name = xingming.Current.Name;
                Debug.WriteLine(dto.Name);
                #endregion
            }
            catch (Exception ex)
            {
                Debug.WriteLine("UIA=>" + ex.Message);
            }
            return dto;
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide(); //或者是this.Visible = false;
                this.notifyIcon1.Visible = true;
            }
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要退出程序吗？", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }
        }

        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;

                this.Hide();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }
        }
    }
}
